<?php 

class ControllerPaymentSisowMC extends Controller {
	private $error = array(); 

	public function index() {
		$this->load->language('payment/sisowmc');
		$this->load->model('setting/setting');

		$this->document->setTitle($this->language->get('heading_title'));

		if ($this->request->server['REQUEST_METHOD'] == 'POST' && $this->validate()) {
			$this->model_setting_setting->editSetting('sisowmc', $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			if (substr(VERSION, 0, 3) == '1.4') {
				if (substr(VERSION, 0, 5) == '1.4.8' || substr(VERSION, 0, 5) == '1.4.9') {
					$this->redirect(HTTPS_SERVER . 'index.php?route=extension/payment&token=' . $this->session->data['token']);
				}
				else {
					$this->redirect(HTTPS_SERVER . 'index.php?route=extension/payment');
				}
			}
			else {
				$this->redirect($this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'));
			}
		}

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_all_zones'] = $this->language->get('text_all_zones');

		$this->data['entry_merchantid'] = $this->language->get('entry_merchantid');
		$this->data['entry_merchantkey'] = $this->language->get('entry_merchantkey');
		$this->data['entry_success'] = $this->language->get('entry_success');
		$this->data['entry_testmode'] = $this->language->get('entry_testmode');
		$this->data['entry_total'] = $this->language->get('entry_total');
		$this->data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_author'] = $this->language->get('entry_author');
		$this->data['entry_version_status'] = $this->language->get('entry_version_status');

		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

		$this->data['tab_general'] = $this->language->get('tab_general');

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		}
		else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->error['username'])) {
			$this->data['error_username'] = $this->error['username'];
		}
		else {
			$this->data['error_username'] = '';
		}

		$this->data['breadcrumbs'] = array();

		if (substr(VERSION, 0, 3) == '1.4') {
			if (substr(VERSION, 0, 5) == '1.4.8' || substr(VERSION, 0, 5) == '1.4.9') {
				$this->data['breadcrumbs'][] = array(
					'href' => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
					'text' => $this->language->get('text_home'),
					'separator' => false
					);

				$this->data['breadcrumbs'][] = array(
					'href' => HTTPS_SERVER . 'index.php?route=extension/payment&token=' . $this->session->data['token'],
					'text' => $this->language->get('text_payment'),
					'separator' => ' :: '
					);

				$this->data['breadcrumbs'][] = array(
					'href' => HTTPS_SERVER . 'index.php?route=payment/sisowmc&token=' . $this->session->data['token'],
					'text' => $this->language->get('heading_title'),
					'separator' => ' :: '
					);
						
				$this->data['action'] = HTTPS_SERVER . 'index.php?route=payment/sisowmc&token=' . $this->session->data['token'];

				$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=extension/payment&token=' . $this->session->data['token'];
			}
			else {
				$this->data['breadcrumbs'][] = array(
					'href' => HTTPS_SERVER . 'index.php?route=common/home',
					'text' => $this->language->get('text_home'),
					'separator' => false
					);

				$this->data['breadcrumbs'][] = array(
					'href' => HTTPS_SERVER . 'index.php?route=extension/payment',
					'text' => $this->language->get('text_payment'),
					'separator' => ' :: '
					);

				$this->data['breadcrumbs'][] = array(
					'href' => HTTPS_SERVER . 'index.php?route=payment/sisowmc',
					'text' => $this->language->get('heading_title'),
					'separator' => ' :: '
					);
						
				$this->data['action'] = HTTPS_SERVER . 'index.php?route=payment/sisowmc';

				$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=extension/payment';
			}
		}
		else {
			$this->data['breadcrumbs'][] = array(
				'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
				'text' => $this->language->get('text_home'),
				'separator' => false
				);

			$this->data['breadcrumbs'][] = array(
				'href' => $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'),
				'text' => $this->language->get('text_payment'),
				'separator' => ' :: '
				);

			$this->data['breadcrumbs'][] = array(
				'href' => $this->url->link('payment/sisowmc', 'token=' . $this->session->data['token'], 'SSL'),
				'text' => $this->language->get('heading_title'),
				'separator' => ' :: '
				);
					
			$this->data['action'] = $this->url->link('payment/sisowmc', 'token=' . $this->session->data['token'], 'SSL');

			$this->data['cancel'] = $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL');
		}

		if (isset($this->request->post['sisowmc_merchantid'])) {
			$this->data['sisowmc_merchantid'] = $this->request->post['sisowmc_merchantid'];
		}
		else {
			$this->data['sisowmc_merchantid'] = $this->config->get('sisowmc_merchantid');
		}

		if (isset($this->request->post['sisowmc_status_success'])) {
			$this->data['sisowmc_status_success'] = $this->request->post['sisowmc_status_success'];
		}
		else {
			$this->data['sisowmc_status_success'] = $this->config->get('sisowmc_status_success'); 
		} 

		if (isset($this->request->post['sisowmc_testmode'])) {
			$this->data['sisowmc_testmode'] = $this->request->post['sisowmc_testmode'];
		}
		else {
			$this->data['sisowmc_testmode'] = $this->config->get('sisowmc_testmode');
		} 

		if (isset($this->request->post['sisowmc_total'])) {
			$this->data['sisowmc_total'] = $this->request->post['sisowmc_total'];
		} else {
			$this->data['sisowmc_total'] = $this->config->get('sisowmc_total'); 
		} 

		if (isset($this->request->post['sisowmc_merchantkey'])) {
			$this->data['sisowmc_merchantkey'] = $this->request->post['sisowmc_merchantkey'];
		}
		else {
			$this->data['sisowmc_merchantkey'] = $this->config->get('sisowmc_merchantkey'); 
		} 
		
		$this->load->model('localisation/geo_zone');
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
		
		if (isset($this->request->post['sisowmc_geo_zone_id'])) {
			$this->data['sisowmc_geo_zone_id'] = $this->request->post['sisowmc_geo_zone_id'];
		} else {
			$this->data['sisowmc_geo_zone_id'] = $this->config->get('sisowmc_geo_zone_id'); 
		} 

		$this->load->model('localisation/order_status');
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

		if (isset($this->request->post['sisowmc_status'])) {
			$this->data['sisowmc_status'] = $this->request->post['sisowmc_status'];
		}
		else {
			$this->data['sisowmc_status'] = $this->config->get('sisowmc_status');
		}

		if (isset($this->request->post['sisowmc_sort_order'])) {
			$this->data['sisowmc_sort_order'] = $this->request->post['sisowmc_sort_order'];
		}
		else {
			$this->data['sisowmc_sort_order'] = $this->config->get('sisowmc_sort_order');
		}
		$this->template = 'payment/sisowmc.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		); 

		$this->response->setOutput($this->render(true), $this->config->get('config_compression')); //v1.3.3+
	}

	private function validate() {
		if (!$this->user->hasPermission('modify', 'payment/sisowmc')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!@$this->request->post['sisowmc_merchantid']) {
			$this->error['merchantid'] = $this->language->get('error_merchantid');
		}

		if (!@$this->request->post['sisowmc_merchantkey']) {
			$this->error['merchantkey'] = $this->language->get('error_merchantkey');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}	
	}
}
?>
